package com.ddh.service.config;
 
import com.ddh.service.enum_.ResultCode;
import com.ddh.service.exception.ServiceException;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * @author young
 * @date 2022/9/12 15:37
 * @description: 获取token并验证
 */
@Component
public class MyJwtInterceptor implements HandlerInterceptor {

 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        if (token == null){
            throw new ServiceException(ResultCode.UNAUTHORIZED);
        }
        return true;
    }
}